﻿$(function () {

    $("#tabs").tabs();

    $('#TeamA, #TeamB').each(function () {
        $(this).change(function () {
            ValidateScore(this);
        });
    });

});

function UpdateResults(){
    var formValues = $('#upcomingGamesForm').serialize();
    var url = "/Games/UpdateResults";
    $.post(url, formValues, function (response) {
        if (response.Status) {
            $('#upcomingGamesResult').removeClass();
            $('#upcomingGamesResult').attr('class', 'ui-state-success ui-corner-all');
            $('#upcomingGamesResult').html('Saved sucessfuly');
        } else {
            $('#upcomingGamesResult').removeClass();
            $('#upcomingGamesResult').attr('class', 'ui-state-error ui-corner-all');
            $('#upcomingGamesResult').html('Unable to save');
        }
        $('#upcomingGamesResult').show();
    });
}

function ValidateScore(element) {
    var score = $(element).val();
    var reg = new RegExp("^[0-9]+$");

    if (score == null || score == '' || !reg.test(score)) {
        $(element).val('0');
    }
    $('#upcomingGamesResult').hide();
}

function OpenMVPSelection(gameId) {
    var url = "/Games/MostValuablePlayer?gameId=" + gameId;

    var actions = {
        "Cancel": function () {
            $(this).dialog("close");
        }
    };

    $("#popup-message").html("<b>Loading...</b>").load(url);

    $("#popup-message").dialog({
        title: "Matches",
        modal: true,
        width: 800,
        height: 600,
        buttons: actions
    });
}

function ShowGameHistoy(teamId) {
    var url = "/Games/History?id=" + teamId;

    var actions = {
        "Close": function () {
            $(this).dialog("close");
        }
    };

    $("#popup-message").html("<b>Loading...</b>").load(url);

    $("#popup-message").dialog({
        title: "History",
        modal: true,
        width: 450,
        height: 550,
        buttons: actions
    });
}

function SetMVP(playerName, playerId, teamName, gameId) {
    $('#popup-message').dialog("close");
    $('#MvpName-' + gameId).html(playerName + " (" + teamName + ")");
    $('#MvpId-' + gameId).val(playerId);
    $('#upcomingGamesResult').hide();
}

function ResultsPreview(gameId, teamA, TeamB) {
    var url = "/Games/ResultsPreview?id=" + gameId;

    var actions = {
        "Close": function () {
            $(this).dialog("close");
        }
    };

    $("#popup-message").html("<b>Loading...</b>").load(url);

    $("#popup-message").dialog({
        title: teamA + " vs " +TeamB,
        modal: true,
        width: 750,
        height: 550,
        buttons: actions
    });
}
